package com.cxh.rmsq.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.CharacterStyle;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.ColorRes;


/**
 * desc：字体颜色
 */
public class TextSpanUtil {

    /**
     * 设置字体前景色
     *
     * @param content
     * @param uniqueArea
     * @param color      颜色资源id
     * @return
     */
    private SpannableStringBuilder spBuilder;
    private String wholeStr, highlightStr;
    private Context mContext;
    private int color;
    private int start = 0, end = 0;

    /**
     * @param context
     * @param wholeStr     全部文字
     * @param highlightStr 改变颜色的文字
     * @param color        颜色
     */
    public TextSpanUtil(Context context, String wholeStr, String highlightStr, int color) {
        this.mContext = context;
        this.wholeStr = wholeStr;
        this.highlightStr = highlightStr;
        this.color = color;
    }

    public TextSpanUtil fillColor() {
        if (!TextUtils.isEmpty(wholeStr) && !TextUtils.isEmpty(highlightStr)) {
            if (wholeStr.contains(highlightStr)) {
                /*
                 *  返回highlightStr字符串wholeStr字符串中第一次出现处的索引。
                 */
                start = wholeStr.indexOf(highlightStr);
                end = start + highlightStr.length();
            } else {
                return null;
            }
        } else {
            return null;
        }
        spBuilder = new SpannableStringBuilder(wholeStr);
        color = mContext.getResources().getColor(color);
        CharacterStyle charaStyle = new ForegroundColorSpan(color);
        spBuilder.setSpan(charaStyle, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public SpannableStringBuilder getResult() {
        if (spBuilder != null) {
            return spBuilder;
        }
        return null;
    }

    @SuppressLint("ResourceAsColor")
    public static SpannableString setSpan(TextView tv, String content, String[] uniqueArea, @ColorRes int color) {
        SpannableString spannableString = new SpannableString(content);
        for (int i = 0; i < uniqueArea.length; i++) {
            int startIndex = 0;
            int endIndex = 0;
            if (content.contains(uniqueArea[i])) {
                startIndex = content.indexOf(uniqueArea[i]);
                endIndex = startIndex + uniqueArea[i].length();
                spannableString.setSpan(new ForegroundColorSpan(color), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }
        if (tv != null) {
            tv.setText(spannableString);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
        return spannableString;
    }

    /**
     * 设置超链接  且设置前景色
     *
     * @param content
     * @param uniqueArea
     * @param color      颜色资源id
     * @param listener
     * @return
     */
    @SuppressLint("ResourceAsColor")
    public static SpannableString setClickableSpan(TextView tv, String content, String uniqueArea, @ColorRes int color, View.OnClickListener listener) {

        SpannableString spannableString = new SpannableString(content);
        int startIndex = 0;
        int endIndex = 0;
        if (content.contains(uniqueArea)) {
            startIndex = content.indexOf(uniqueArea);
            endIndex = startIndex + uniqueArea.length();
            //可以为多部分设置超链接
            spannableString.setSpan(new Clickable(listener), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new ForegroundColorSpan(color), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        if (tv != null) {
            tv.setText(spannableString);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
        return spannableString;
    }

    /**
     * 设置多种文字前景色  及跳转
     *
     * @param tv
     * @param content
     * @param strings1  前景色修改  数组
     * @param color1    前景色修改  颜色
     * @param strings2  链接带前景色 文本数组
     * @param color2    链接  颜色数组
     * @param listeners 链接跳转监听数组
     * @return
     */
    @SuppressLint("ResourceAsColor")
    public static SpannableString setMultiSpan(TextView tv, String content, String[] strings1, @ColorRes int color1, String[] strings2, @ColorRes int color2, View.OnClickListener[] listeners) {
        SpannableString spannableString = new SpannableString(content);
        for (int i = 0; i < strings1.length; i++) {
            if (strings1[i].isEmpty()) {
                continue;
            }
            int startIndex = 0;
            int endIndex = 0;
            if (content.contains(strings1[i])) {
                startIndex = content.indexOf(strings1[i]);
                endIndex = startIndex + strings1[i].length();
                spannableString.setSpan(new ForegroundColorSpan(color1), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }

        for (int i = 0; i < strings2.length; i++) {
            if (strings2[i].isEmpty()) {
                continue;
            }
            int startIndex = 0;
            int endIndex = 0;
            if (content.contains(strings2[i])) {
                startIndex = content.indexOf(strings2[i]);
                endIndex = startIndex + strings2[i].length();
                //可以为多部分设置超链接
                if (listeners[i] != null) {
                    spannableString.setSpan(new Clickable(listeners[i]), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                }
                spannableString.setSpan(new ForegroundColorSpan(color2), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }
        if (tv != null) {
            tv.setText(spannableString);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
        return spannableString;
    }

    @SuppressLint("ResourceAsColor")
    public static SpannableString setMultiSpan(TextView tv, String content, String[] strings1, @ColorRes int color1, String[] strings2, @ColorRes int color2) {
        SpannableString spannableString = new SpannableString(content);
        for (int i = 0; i < strings1.length; i++) {
            if (strings1[i].isEmpty()) {
                continue;
            }
            int startIndex = 0;
            int endIndex = 0;
            if (content.contains(strings1[i])) {
                startIndex = content.indexOf(strings1[i]);
                endIndex = startIndex + strings1[i].length();
                spannableString.setSpan(new ForegroundColorSpan(color1), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }

        for (int i = 0; i < strings2.length; i++) {
            if (strings2[i].isEmpty()) {
                continue;
            }
            int startIndex = 0;
            int endIndex = 0;
            if (content.contains(strings2[i])) {
                startIndex = content.indexOf(strings2[i]);
                endIndex = startIndex + strings2[i].length();
                //可以为多部分设置超链接
//                if (listeners[i] != null) {
//                    spannableString.setSpan(new Clickable(listeners[i]), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//                }
                spannableString.setSpan(new ForegroundColorSpan(color2), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }
        if (tv != null) {
            tv.setText(spannableString);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
        return spannableString;
    }

    public static class Clickable extends ClickableSpan implements View.OnClickListener {
        private final View.OnClickListener mListener;

        public Clickable(View.OnClickListener listener) {
            mListener = listener;
        }

        @Override
        public void onClick(View view) {
            mListener.onClick(view);
        }
    }
}
